package com.hllxd.hism.passport.login.gateway.dao.redis;

import com.hllxd.hism.passport.login.gateway.config.redis.RedisAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yuanhao
 * @date 2020-05-12 11:29
 */
@Slf4j @Service("accessTokenRedisMapper2")
public class AccessTokenRedisMapperImpl implements AccessTokenRedisMapper {
    @Autowired private RedisAdapter redisAdapter;

    /** 有效时间30天 */
    private static final long EXPIRE_DATE = 30 * 24 * 60 * 60 * 1000L;

    /** TOKEN 前缀 */
    private static final String MIS_TOKEN_PREFIX = "inward_hllopen_token:";
    private static final String MIS_TOKEN_INFO_PREFIX = "inward_hllopen_token_i:";

    /** 拼接命名空间前缀 */
    private String buildKey(String openID, String appKey, String appSecret) {
        return MIS_TOKEN_PREFIX + openID + "^" + appKey + "^" + appSecret;
    }

    private String buildKey2(String key) {
        return MIS_TOKEN_INFO_PREFIX + key;
    }

    /** 拼接命名空间前缀 */
    private String buildKey(Integer plat, String phoneNumber) {
        return MIS_TOKEN_PREFIX + plat + ":" + phoneNumber;
    }



    @Override public void setAccessToken(Integer plat, String phoneNumber, String accessTokenJson) {
        redisAdapter.setValue(buildKey(plat, phoneNumber), accessTokenJson, EXPIRE_DATE);
    }

    @Override public String getAccessToken(Integer plat, String phoneNumber) {
        return redisAdapter.getValue(buildKey(plat, phoneNumber));
    }

    @Override public void setTokenInfo(String token, String value) {
        redisAdapter.setValue(buildKey2(token), value, EXPIRE_DATE);
    }

    @Override public String getTokenInfo(String token) {
        return redisAdapter.getValue(buildKey2(token));
    }
}
